﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core;
using DalatCooking.Core.Helper;

public partial class admin_Document_Add_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        LoadParentListBox(lbCategories, 0);
        lbCategories.SelectedIndex = 0;
        BinAuthors();

        if (DocumentID != -1)
        {
            BinDataInfo();
        }
        base.OnInit(e);
    }
    public void BinAuthors()
    {
        ddlAuthor.DataSource = PersonManager.FindAll();
        ddlAuthor.DataTextField = "UserName";
        ddlAuthor.DataValueField = "PersonId";
        ddlAuthor.DataBind();
    }
    void BinDataInfo()
    {
        Document c = DocumentManager.FindById(DocumentID);
        if (c != null)
        {
            txtName.Text = c.Name;
            txtDescription.Text = c.Description;
            txtBody.Text = c.Body;
            txtDate.Text = c.DateCreated.ToString();
            txtSlug.Text = c.UrlFriendly;
            imgthumb.Src = c.Picture;
            txtPicture.Text = c.Picture;
            txtFileUrl.Text = c.FileUrl;
            cbIsActive.Checked = (bool)c.IsActive;
            // cbEnableComments.Checked = (bool)c.AllowComment;
            // cbIsFrontPage.Checked = (bool)c.IsFrontPage;
            txtSortIndex.Text = c.SortIndex.ToString();
            lbCategories.SelectedValue = c.CourseId.ToString();

        }
    }

    public int DocumentID
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["DocumentID"]))
            {
                return ConvertObject.ToInt(Request.QueryString["DocumentID"]);
            }
            return -1;
        }
    }
    int level = -1;
    void LoadParentListBox(DropDownList ddl, int parentID)
    {
        level++;
        System.Text.StringBuilder appender = new System.Text.StringBuilder();
        for (int j = 0; j < level; j++)
        {
            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        if (level > 0)
        {
            appender.Append("|__");
        }
        foreach (var i in CourseManager.FindAll().Where(c => c.IsActive == true))
        {
            ListItem l = new ListItem();
            l.Value = i.CourseId.ToString();
            l.Text = HttpUtility.HtmlDecode(appender.ToString()) + i.Name;
            l.Attributes.Add("data-image", "ThumbnailHandler.axd?img=" + i.Picture + "");
            l.Attributes.Add("data-description", HttpUtility.HtmlDecode(appender.ToString()) + i.Name);

            ddl.Items.Add(l);
            // ddl.Items.Add(new ListItem(Server.HtmlDecode(appender.ToString()) + i.Name, i.CategoryId.ToString()));
          //  LoadParentListBox(ddl, i.CategoryId);
        }
        level--;
    }
    void Insert()
    {
        Document c = new Document();
        c.CourseId = ConvertObject.ToInt(lbCategories.SelectedValue);
        c.DocumentId = -1;
        c.Name = txtName.Text;
        c.UrlFriendly = StringHelper.RemoveDiacriticsURL(txtName.Text);
        c.FileType = ddlFilType.Text;
        c.FileUrl = txtFileUrl.Text;
        c.Description = txtDescription.Text;
        c.Body = txtBody.Text;
        c.Picture = txtPicture.Text;
        c.IsActive = cbIsActive.Checked;
        c.CreatedBy = ddlAuthor.Text;
        c.CreatedById = ConvertObject.ToInt(ddlAuthor.SelectedValue);

        if (DocumentManager.Insert(c))
            Response.Redirect("Default.aspx");
        /*  @DocumentId int output,       
         @CourseId int,
         @FileName nvarchar (50),
         @UrlFriendly nvarchar (255),
         @FileUrl nvarchar (255),
         @Picture nvarchar (255),
         @FileType nvarchar (50),
         @Description nvarchar (50),
         @Body ntext ,
         @CreatedById int ,
         @CreteadBy nvarchar (50),
         @IsActive bit */

    }
    void Update()
    {
        Document c = new Document();
        c.DocumentId = DocumentID;
        c.CourseId = ConvertObject.ToInt(lbCategories.SelectedValue);
        c.Name = txtName.Text;
        c.UrlFriendly = StringHelper.RemoveDiacriticsURL(txtName.Text);
        c.FileType = ddlFilType.Text;
        c.FileUrl = txtFileUrl.Text;
        c.Description = txtDescription.Text;
        c.Body = txtBody.Text;
        c.Picture = txtPicture.Text;
        c.IsActive = cbIsActive.Checked;
        c.CreatedBy = ddlAuthor.Text;
        c.CreatedById = ConvertObject.ToInt(ddlAuthor.SelectedValue);
        
        c.SortIndex = ConvertObject.ToInt(txtSortIndex.Text);
        if (DocumentManager.Update(c))
            Response.Redirect("Default.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (DocumentID != -1)
            Update();
        else Insert();
    }
    protected void btnCategory_Click(object sender, EventArgs e)
    {

    }
  
}